package cn.globalph.housekeeper.widgets

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import cn.globalph.housekeeper.R

class SelectNumberView : ConstraintLayout {
    private var leftLayout: LinearLayout
    private var reduceTv: ImageView
    private var increaseTv: ImageView
    private var numberTv: TextView
    private var nameTv: TextView

    var number = 1
    set(value) {
        field = value
        numberTv.text = value.toString()
    }

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {

        val view = View.inflate(context, R.layout.select_number, this)
        leftLayout = view.findViewById(R.id.left_layout)
        reduceTv = view.findViewById(R.id.reduce_tv)
        increaseTv = view.findViewById(R.id.increase_tv)
        numberTv = view.findViewById(R.id.number_tv)
        nameTv = view.findViewById(R.id.name_tv)

        val ta = context.obtainStyledAttributes(attributeSet!!, R.styleable.SelectNumberView)
        val typeName = ta.getString(R.styleable.SelectNumberView_type_name)
        if (typeName != null) nameTv.text = typeName


        numberTv.text = number.toString()
        reduceTv.setOnClickListener {
            if (number > 0) {
                number--
            }
            numberTv.text = number.toString()
        }

        increaseTv.setOnClickListener {
            number++
            numberTv.text = number.toString()
        }

        ta.recycle()
    }
}